<?php
//	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//		$upfile = 'upfile';
//		$upimage = upimage($upfile);
//	}


function upimage($upfile,$imgthumb=1){
if($imgthumb!=="1"){
	$imgthumb=$imgthumb;	//是否生成缩略图(1为生成,其他为不生成);
}
//上传文件类型列表
$uptypes=array(
	'image/jpg',
	'image/jpeg',
	'image/png',
	'image/jpeg',
	'image/gif',
	'image/bmp',
	'image/x-png');
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE
$uptimey=date("Y/",$_SERVER['REQUEST_TIME']);
$uptimem=date("m/",$_SERVER['REQUEST_TIME']);
$uptimed=date("d/",$_SERVER['REQUEST_TIME']);
$uptimeall=date("Y/m/d/",$_SERVER['REQUEST_TIME']);
$foldera="ws_user_up/image/";
$folder=$foldera.$uptimey.$uptimem.$uptimed; //上传文件路径
$newheight = '200';
$newwidth = '200';
$thumb = 'thumb/';
$imgpreview=1;      //是否生成预览图(1为生成,其他为不生成);
$imgpreviewsize=1/2;    //缩略图比例
	//是否存在文件
	if (!is_uploaded_file($_FILES["$upfile"]["tmp_name"])){
		echo "图片不存在!";
		exit;
	}
	$file = $_FILES["$upfile"];
	//检查文件大小
	if($max_file_size < $file["size"]){
		echo "文件太大!";
		exit;
	}
	//检查文件类型
	if(!in_array($file["type"], $uptypes)){
		echo "文件类型不符!".$file["type"];
		exit;
	}
	if(!file_exists($foldera))
		mkdir($foldera);
	if(!file_exists($foldera.$uptimey))
		mkdir($foldera.$uptimey);
	if(!file_exists($foldera.$uptimey.$uptimem))
		mkdir($foldera.$uptimey.$uptimem);
	if(!file_exists($folder))
		mkdir($folder);
	if(!file_exists($folder.$thumb))
		mkdir($folder.$thumb);

	$filename=$file["tmp_name"];
	$image_size = getimagesize($filename);
	$pinfo=pathinfo($file["name"]);
	$ftype=$pinfo['extension'];
	$destination = $folder.time().".".$ftype;
	if (file_exists($destination) && $overwrite != true){
		echo "同名文件已经存在了";
		exit;
	}
	if(!move_uploaded_file ($filename, $destination)){
		echo "移动文件出错";
		exit;
	}
	$pinfo=pathinfo($destination);
	$fname=$pinfo["basename"];
	$upimage[0] = " <font color=red>已经成功上传</font><br>文件名:  <font color=blue>".$folder.$fname."</font><br>";
	$upimage[1] = " 宽度:".$image_size[0]." 长度:".$image_size[1]."<br> 大小:".$file["size"]." bytes";
	if($imgthumb==1){
		$image = thumb($fname,$folder,$thumb,$newwidth,$newheight);
	}
	if($imgpreview==1){
		$upimage[2] = "<br>图片预览:<br>
		<img src='$folder$thumb$fname' width='($image_size[0]*$imgpreviewsize)' height='($image_size[1]*$imgpreviewsize)'>";
		
	}
	$upimage[3] = $uptimeall.$fname;
	$upimage[4] = $uptimeall.$thumb.$fname;
	return $upimage;
}
//缩略图动作
function thumb($name,$path,$thumb,$newwidth,$newheight){
$tpath = $path.$thumb.$name;
$filename = $path.$name;
$percent = 0.5;
//原图大小
list($width, $height) = getimagesize($filename);
//缩略图大小
//$newwidth = $width * $percent;
//$newheight = $height * $percent;
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,$tpath);
}
?>
